package com.lpwa.web.controller.system;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.lpwa.framework.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.lpwa.common.annotation.Log;
import com.lpwa.common.enums.BusinessType;
import com.lpwa.system.domain.DeviceModel;
import com.lpwa.system.service.IDeviceModelService;
import com.lpwa.common.core.controller.BaseController;
import com.lpwa.common.core.page.TableDataInfo;
import com.lpwa.common.core.domain.AjaxResult;
import com.lpwa.common.utils.poi.ExcelUtil;
import org.springframework.web.multipart.MultipartFile;

/**
 * 设备型号 信息操作处理
 * 
 * @author tanmu
 * @date 2019-06-04
 */
@Controller
@RequestMapping("/system/deviceModel")
public class DeviceModelController extends BaseController
{
    private String prefix = "system/deviceModel";
	
	@Autowired
	private IDeviceModelService deviceModelService;
	
	@RequiresPermissions("system:deviceModel:view")
	@GetMapping()
	public String deviceModel()
	{
	    return prefix + "/deviceModel";
	}
	
	/**
	 * 查询设备型号列表
	 */
	@RequiresPermissions("system:deviceModel:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(DeviceModel deviceModel)
	{
		startPage();
        List<DeviceModel> list = deviceModelService.selectDeviceModelList(deviceModel);
		return getDataTable(list);
	}
	/**
	 * 导出设备型号列表
	 */
	@RequiresPermissions("system:deviceModel:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(DeviceModel deviceModel)
    {
    	List<DeviceModel> list = deviceModelService.selectDeviceModelList(deviceModel);
        ExcelUtil<DeviceModel> util = new ExcelUtil<DeviceModel>(DeviceModel.class);
        return util.exportExcel(list, "deviceModel");
    }
	
	/**
	 * 新增设备型号
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存设备型号
	 */
	@RequiresPermissions("system:deviceModel:add")
	@Log(title = "设备型号", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(DeviceModel deviceModel)
	{
		deviceModel.setCreateBy(ShiroUtils.getLoginName());
		return toAjax(deviceModelService.insertDeviceModel(deviceModel));
	}

	/**
	 * 修改设备型号
	 */
	@GetMapping("/edit/{modelId}")
	public String edit(@PathVariable("modelId") Long modelId, ModelMap mmap)
	{
		DeviceModel deviceModel = deviceModelService.selectDeviceModelById(modelId);
		mmap.put("deviceModel", deviceModel);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存设备型号
	 */
	@RequiresPermissions("system:deviceModel:edit")
	@Log(title = "设备型号", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(DeviceModel deviceModel)
	{
		deviceModel.setUpdateBy(ShiroUtils.getLoginName());
		return toAjax(deviceModelService.updateDeviceModel(deviceModel));
	}
	
	/**
	 * 删除设备型号
	 */
	@RequiresPermissions("system:deviceModel:remove")
	@Log(title = "设备型号", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(deviceModelService.deleteDeviceModelByIds(ids));
	}
	/**
	 * 通过代号modelCode查询设备型号
	 */
	@PostMapping("/getDeviceModelByCode")
	@ResponseBody
	public AjaxResult getDeviceModelByCode(@RequestParam String modelCode)
	{
		DeviceModel deviceModel = deviceModelService.selectDeviceModelByCode(modelCode);

		return AjaxResult.success(JSON.toJSON(deviceModel));
	}

	@Log(title = "设备型号管理", businessType = BusinessType.IMPORT)
	@RequiresPermissions("system:deviceModel:import")
	@PostMapping("/importData")
	@ResponseBody
	public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
	{
		ExcelUtil<DeviceModel> util = new ExcelUtil<DeviceModel>(DeviceModel.class);
		List<DeviceModel> modelList = util.importExcel(file.getInputStream());
		String operName = ShiroUtils.getSysUser().getLoginName();//操作人
		String message = deviceModelService.importData(modelList, updateSupport, operName);
		return AjaxResult.success(null);
	}
	@RequiresPermissions("system:deviceModel:view")
	@GetMapping("/importTemplate")
	@ResponseBody
	public AjaxResult importTemplate()
	{
		ExcelUtil<DeviceModel> util = new ExcelUtil<DeviceModel>(DeviceModel.class);
		return util.importTemplateExcel("设备型号数据");
	}
	@PostMapping("/checkModelCodeUnique")
	@ResponseBody
	public String checkModelCodeUnique(@RequestParam("modelCode") String modelCode)
	{
		Boolean bool = deviceModelService.checkLableIdUnique(modelCode);
		if (bool){
			return "0";
		}
		return "1";
	}
}
